package com.sne.common.util;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sne.common.annotation.Dict;
import com.sne.common.annotation.User;
import com.sne.sys.entity.SysUser;
import com.sne.sys.service.ISysDictItemService;
import com.sne.sys.service.ISysUserService;
import com.sne.sys.vo.SysDictItemVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.*;

/**
 * 转换字典，用户实体工具类
 *
 * @author fanghh
 * @date 2020/10/9
 */
@Slf4j
@Component
public class ConvertUtils implements InitializingBean {

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private ISysDictItemService iSysDictItemService;

    private static ISysUserService userService;

    private static ISysDictItemService dictItemService;

    /**
     * 将待有User，Dict注解进行转换
     *
     * @param obj 对象
     * @return JSONObject
     */
    public static Map convert(Object obj) {
        Map<String,Object> map = new HashMap<>();
        Field[] fields = FieldUtils.getAllFields(obj.getClass());
        for (Field field : fields) {
            try {
                Dict dict = field.getDeclaredAnnotation(Dict.class);
                field.setAccessible(true);
                map.put(field.getName(),field.get(obj));
                if (dict != null) {
                    String code = (String) field.get(obj);
                    SysDictItemVO itemCode = dictItemService.findDictItemCode(dict.value(), code);
                    if (itemCode != null) {
                        map.put(field.getName() + "Name", itemCode.getItemName());
                    }
                }
                User user = field.getDeclaredAnnotation(User.class);
                if (user != null) {
                    Long id = (Long) field.get(obj);
                    SysUser sysUser = userService.getById(id);
                    if (sysUser != null) {
                        map.put(field.getName().replace("Id", "") + "Name", sysUser.getUserName());
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                log.error("异常信息 ex={}", e.getMessage(), e);
            }
        }
        return map;
    }


    /**
     * 转换list
     *
     * @param collection 转换集合
     * @return Collection
     */
    public static Collection<Map> convert(Collection collection) {
        Collection<Map> objects = new ArrayList<>();
        collection.forEach(x -> objects.add(convert(x)));
        return objects;
    }

    public static Page convert(Page page){
        List records = page.getRecords();
        List<Map> maps = (List<Map>) convert(records);
        page.setRecords(maps);
        return page;
    }

    @Override
    public void afterPropertiesSet() {
        userService = this.iSysUserService;
        dictItemService = this.iSysDictItemService;
    }
}
